package com.dex4d.ui.wallet.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.model.PinItem;
import com.dex4d.utils.Commons;
import com.dex4d.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * Created by chengyuchun on 2018/10/27.
 */
public class PinActivity extends AppToolBarBaseActivity implements Commons.BUNDLE_KEYS {

    @BindView(R.id.pin_1)
    EditText pin1;

    @BindView(R.id.pin_2)
    EditText pin2;

    @BindView(R.id.pin_3)
    EditText pin3;

    @BindView(R.id.pin_4)
    EditText pin4;

    @BindView(R.id.pin_5)
    EditText pin5;

    @BindView(R.id.pin_6)
    EditText pin6;

    @BindView(R.id.v_cover)
    FrameLayout v_cover;

    private List<PinItem> pinList = new ArrayList<PinItem>();

    @Override
    protected void initToolbar() {
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pin;
    }

    @Override
    protected void initVariables() {
        PinItem item0 = new PinItem();
        item0.setId(0);
        item0.setEditText(pin1);

        PinItem item1 = new PinItem();
        item1.setId(1);
        item1.setEditText(pin2);

        PinItem item2 = new PinItem();
        item2.setId(2);
        item2.setEditText(pin3);

        PinItem item3 = new PinItem();
        item3.setId(3);
        item3.setEditText(pin4);

        PinItem item4 = new PinItem();
        item4.setId(4);
        item4.setEditText(pin5);

        PinItem item5 = new PinItem();
        item5.setId(5);
        item5.setEditText(pin6);

        pinList.add(item0);
        pinList.add(item1);
        pinList.add(item2);
        pinList.add(item3);
        pinList.add(item4);
        pinList.add(item5);
    }

    @Override
    protected void loadData() {
        for( int i=0;i<pinList.size();i++){
            setListener(pinList.get(i));
        }

        pinList.get(0).getEditText().requestFocus();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager imm = (InputMethodManager)
                       getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(pinList.get(0).getEditText(), InputMethodManager.SHOW_FORCED);
            }
        },300);

        v_cover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(PinItem pinItem:pinList){
                    if(TextUtils.isEmpty(pinItem.getEditText().getText().toString())){
                        InputMethodManager imm = (InputMethodManager)
                                getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(pinItem.getEditText(), InputMethodManager.SHOW_FORCED);
                        return;
                    }
                }
            }
        });
    }

    private void setListener(final PinItem item){
        final EditText pin = item.getEditText();
        pin.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence text, int start, int before, int count) {


            }

            @Override
            public void beforeTextChanged(CharSequence text, int start, int count,int after) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //删除操作
                if(editable.length() == 0) {
                    pin.setBackgroundResource(R.drawable.pin_dot_empty_bg);
                    pin.requestFocus();
                    LogUtils.d(item.getId());
                    return;
                }

                //输入操作
                int id = item.getId();
                if(editable.length() >0 && id<6){
                    pin.setBackgroundResource(R.drawable.pin_dot_filled_bg);
                    if(id+1<6){
                        pinList.get(id+1).getEditText().setText("");
                        pinList.get(id+1).getEditText().requestFocus();
                    }else if(id == 5){
                        operatePins();
                    }
                }
            }
        });


        pin.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
                if(keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_UP) {
                    if(!TextUtils.isEmpty(pin.getText())){
                        pin.setText("");
                    }else {
                        int id = item.getId()-1;
                        if(id>=0){
                            pinList.get(id).getEditText().setText("");
                        }
                    }
                }
                return true;
            }
        });
    }

    protected void operatePins(){
        Bundle bundle = getIntent().getExtras();
        bundle.putString(PIN_CODE,getPins());
        openActivity(ConfirmPinActivity.class,bundle);
        finish();
    }

    protected String getPins(){
        StringBuffer pins = new StringBuffer();
        for(PinItem item:pinList){
            pins.append(item.getEditText().getText().toString());
        }
        return pins.toString();
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, PinActivity.class);
        activity.startActivity(intent);
    }
}
